﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace RNT.Common.Utils
{
    /// <summary>
    /// 控件委托辅助类，跨线程访问
    /// </summary>
    public class CrossThreadCalls
    {
        /// <summary>
        /// 跨线程设置控件属性值委托类型定义
        /// </summary>
        delegate void SetControlPropertyCallBack(System.Windows.Forms.Control control, string name, object value);
        /// <summary>
        /// 跨线程设置控件属性值
        /// </summary>
        public static void SetControlProperty(System.Windows.Forms.Control control, string name, object value)
        {
            try
            {
                if (control.InvokeRequired)
                {
                    SetControlPropertyCallBack CallBack = new SetControlPropertyCallBack(SetControlProperty);
                    control.Invoke(CallBack, new object[] { control, name, value });
                }
                else
                {
                    Type type = control.GetType();
                    type.InvokeMember(name,
                        BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance,
                      null, control, new object[] { value });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 跨线程设置控件属性值委托类型定义
        /// </summary>
        delegate object GetControlPropertyCallBack(System.Windows.Forms.Control control, string name);
        /// <summary>
        /// 跨线程读取控件属性值
        /// </summary>
        public static object GetControlProperty(System.Windows.Forms.Control control, string name)
        {
            try
            {
                if (control.InvokeRequired)
                {
                    GetControlPropertyCallBack CallBack = new GetControlPropertyCallBack(GetControlProperty);
                    return control.Invoke(CallBack, new object[] { control, name });
                }
                else
                {
                    Type type = control.GetType();
                    return type.InvokeMember(name,
                        BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance,
                      null, control, null);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 跨线程调用控件方法委托类型定义
        /// </summary>
        delegate object CallControlMethodCallBack(System.Windows.Forms.Control control, string name, object[] parameters);
        /// <summary>
        /// 跨线程调用控件方法
        /// </summary>
        public static object CallControlMethod(System.Windows.Forms.Control control, string name, object[] parameters)
        {
            try
            {
                if (control.InvokeRequired)
                {
                    CallControlMethodCallBack CallBack = new CallControlMethodCallBack(CallControlMethod);
                    return control.Invoke(CallBack, new object[] { control, name, parameters });
                }
                else
                {
                    Type type = control.GetType();

                    MethodInfo methodInfo = type.GetMethod(name);
                    return methodInfo.Invoke(control, parameters);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
